//
//  PickerOneView.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/12.
//

#import "PickerOneView.h"
#import "YearMonthDayPickerView.h"
#import "FaceViewController.h"

#define kWholeViewHeight  DimensionBaseIphone6(430)

@interface PickerOneView ()

@property (nonatomic) NSString *nameStr,*idNoStr,*dateStr;

@property (nonatomic) UIView *wholeView;

@property (nonatomic) CreditTextFiled *fullNameFiled,*IDNoFiled,*DateFiled;

@end

@implementation PickerOneView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = kBACK_SHADOWCOLOR;
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickCacel) name:@"HideAlert" object:nil];
        
    }
    return self;
}

- (void)keyboardShow:(NSNotification *)noti{
    
    NSDictionary *userInfo = [noti userInfo];
    
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];

    CGRect keyboardRect = [aValue CGRectValue];
    
    CGFloat height = keyboardRect.size.height;
    
    CGFloat width = keyboardRect.size.width;

    CGFloat alertViewHeight = self.wholeView.frame.origin.y + self.wholeView.size.height;
   
    CGFloat y = height - (kScreenHeight - alertViewHeight);

    if (y > 0) {
        self.frame = CGRectMake(0, -y + DimensionBaseIphone6(80), kScreenWidth, kScreenHeight);
    }
    else{
        self.frame = CGRectMake(0, 0, kScreenWidth,kScreenHeight);
    }

}

- (void)keyboardHide:(NSNotification *)noti{
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
}

- (void)pickViewWithNameStr:(NSString *)nameStr andIDNoStr:(NSString *)idNoStr andDateStr:(NSString *)dateStr{
    self.nameStr = nameStr;
    self.idNoStr = idNoStr;
    self.dateStr = dateStr;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [self createBottomView];
    
}

- (void)createBottomView{
    self.wholeView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, kWholeViewHeight)];
    self.wholeView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.wholeView];
    
    UIImage *img = [UIImage imageNamed:@"icon_idf13"];
    UIImageView *topImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, DimensionBaseIphone6(img.size.height))];
    [self.wholeView addSubview:topImgView];
    topImgView.image = img;
    topImgView.userInteractionEnabled = YES;
    
   
    UILabel *titleLab = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:16] color:[UIColor colorWithHexString:@"#022D06"]];
    [topImgView addSubview:titleLab];
    titleLab.textAlignment = 1;
    titleLab.frame = CGRectMake(0, DimensionBaseIphone6(20), topImgView.size.width, DimensionBaseIphone6(20));
    titleLab.text = @"Identity Information";
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenWidth - 18 - 24, topImgView.bottom - 24, 24, 24)];
    [cancelBtn setImage:[UIImage imageNamed:@"icon_idf14"] forState:UIControlStateNormal];
    [cancelBtn setImage:[UIImage imageNamed:@"icon_idf14"] forState:UIControlStateSelected];
    [cancelBtn setImage:[UIImage imageNamed:@"icon_idf14"] forState:UIControlStateHighlighted];
    [cancelBtn addTarget:self action:@selector(clickCacel) forControlEvents:UIControlEventTouchUpInside];
    [topImgView addSubview:cancelBtn];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, topImgView.bottom, kScreenWidth, self.wholeView.size.height - topImgView.bottom)];
    backView.backgroundColor = [UIColor whiteColor];
    [self.wholeView addSubview:backView];
    
    UILabel *lab1 = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:13] color:[UIColor colorWithHexString:@"#638473"]];
    lab1.text = @"Full Name";
    [backView addSubview:lab1];
    lab1.frame = CGRectMake(15, DimensionBaseIphone6(10), 100, DimensionBaseIphone6(44));
    
    self.fullNameFiled = [[CreditTextFiled alloc] initWithFrame:CGRectMake(15, lab1.bottom, backView.size.width - 30, DimensionBaseIphone6(44))];
    [backView addSubview:self.fullNameFiled];
    self.fullNameFiled.textColor = [UIColor colorWithHexString:@"#022D06"];
    self.fullNameFiled.placeholder = @"Please Enter";
    self.fullNameFiled.font = [UIFont fontWithName:kFuturaMedium size:13];
    [self.fullNameFiled addLeftViewWithTitle:@"" leftMargin:10 leftWidth:10 leftColor:[UIColor clearColor] leftFont:[UIFont systemFontOfSize:10] leftViewMode:UITextFieldViewModeAlways];
    NSMutableAttributedString *placeholderCapString1 = [[NSMutableAttributedString alloc] initWithString:self.fullNameFiled.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#F1F3F2"],NSFontAttributeName:[UIFont fontWithName:kFuturaMedium size:13]}];
    self.fullNameFiled.attributedPlaceholder = placeholderCapString1;
    self.fullNameFiled.layer.cornerRadius = 50/2;
    self.fullNameFiled.layer.masksToBounds = YES;
    self.fullNameFiled.layer.borderWidth = 1;
    self.fullNameFiled.layer.borderColor = [UIColor colorWithHexString:@"#F1F3F2"].CGColor;
    
    UILabel *lab2 = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:13] color:[UIColor colorWithHexString:@"#638473"]];
    lab2.text = @"ID No.";
    [backView addSubview:lab2];
    lab2.frame = CGRectMake(15, self.fullNameFiled.bottom, 100, DimensionBaseIphone6(44));
    
    
    self.IDNoFiled = [[CreditTextFiled alloc] initWithFrame:CGRectMake(15, lab2.bottom, backView.size.width - 30, DimensionBaseIphone6(44))];
    [backView addSubview:self.IDNoFiled];
    self.IDNoFiled.textColor = [UIColor colorWithHexString:@"#022D06"];
    self.IDNoFiled.placeholder = @"Please Enter";
    self.IDNoFiled.font = [UIFont fontWithName:kFuturaMedium size:13];
    [self.IDNoFiled addLeftViewWithTitle:@"" leftMargin:10 leftWidth:10 leftColor:[UIColor clearColor] leftFont:[UIFont systemFontOfSize:10] leftViewMode:UITextFieldViewModeAlways];
    NSMutableAttributedString *placeholderCapString2 = [[NSMutableAttributedString alloc] initWithString:self.IDNoFiled.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#F1F3F2"],NSFontAttributeName:[UIFont fontWithName:kFuturaMedium size:13]}];
    self.IDNoFiled.attributedPlaceholder = placeholderCapString2;
    self.IDNoFiled.layer.cornerRadius = 50/2;
    self.IDNoFiled.layer.masksToBounds = YES;
    self.IDNoFiled.layer.borderWidth = 1;
    self.IDNoFiled.layer.borderColor = [UIColor colorWithHexString:@"#F1F3F2"].CGColor;
    
    self.IDNoFiled.keyboardType = UIKeyboardTypeNumberPad;
    
    UILabel *lab3 = [Utility createLabel:[UIFont fontWithName:kFuturaMedium size:13] color:[UIColor colorWithHexString:@"#638473"]];
    lab3.text = @"Date Of Birth";
    [backView addSubview:lab3];
    lab3.frame = CGRectMake(15, self.IDNoFiled.bottom, 100, DimensionBaseIphone6(44));
    
    self.DateFiled = [[CreditTextFiled alloc] initWithFrame:CGRectMake(15, lab3.bottom, backView.size.width - 30, DimensionBaseIphone6(44))];
    [backView addSubview:self.DateFiled];
    self.DateFiled.textColor = [UIColor colorWithHexString:@"#022D06"];
    self.DateFiled.placeholder = @"Please Enter";
    self.DateFiled.font = [UIFont fontWithName:kFuturaMedium size:13];
    [self.DateFiled addLeftViewWithTitle:@"" leftMargin:10 leftWidth:10 leftColor:[UIColor clearColor] leftFont:[UIFont systemFontOfSize:10] leftViewMode:UITextFieldViewModeAlways];
    NSMutableAttributedString *placeholderCapString3 = [[NSMutableAttributedString alloc] initWithString:self.DateFiled.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#F1F3F2"],NSFontAttributeName:[UIFont fontWithName:kFuturaMedium size:13]}];
    self.DateFiled.attributedPlaceholder = placeholderCapString3;
    self.DateFiled.layer.cornerRadius = 50/2;
    self.DateFiled.layer.masksToBounds = YES;
    self.DateFiled.layer.borderWidth = 1;
    self.DateFiled.layer.borderColor = [UIColor colorWithHexString:@"#F1F3F2"].CGColor;
    
  
    self.DateFiled.userInteractionEnabled = NO;
    
    
    UIImageView *rightImgV = [[UIImageView alloc] initWithFrame:CGRectMake(backView.frame.size.width - 35 - 16, 0, 16, 14)];
    rightImgV.centerY = self.DateFiled.centerY;
    [backView addSubview:rightImgV];
    rightImgV.image = [UIImage imageNamed:@"icon_idf12"];
    
    UIView *shadeV = [[UIView alloc] initWithFrame:self.DateFiled.frame];
    shadeV.userInteractionEnabled = YES;
    shadeV.backgroundColor = [UIColor clearColor];
    [backView addSubview:shadeV];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickDate)];
    [shadeV addGestureRecognizer:tap];
    
    
    UIButton *redBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.DateFiled.bottom, kScreenWidth, DimensionBaseIphone6(36))];
    [redBtn setTitle:[NSString stringWithFormat:@"%@",@"  *Please confirm relevant information"] forState:UIControlStateNormal];
    [redBtn setTitle:[NSString stringWithFormat:@"%@",@"  *Please confirm relevant information"] forState:UIControlStateSelected];
    [redBtn setTitle:[NSString stringWithFormat:@"%@",@"  *Please confirm relevant information"] forState:UIControlStateHighlighted];
    [redBtn setImage:[UIImage imageNamed:@"icon_idf15"] forState:UIControlStateNormal];
    [redBtn setImage:[UIImage imageNamed:@"icon_idf15"] forState:UIControlStateSelected];
    [redBtn setImage:[UIImage imageNamed:@"icon_idf15"] forState:UIControlStateSelected];
    redBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:12];
    [redBtn setTitleColor:[UIColor colorWithHexString:@"#FE3A2F"] forState:UIControlStateNormal];
    [redBtn setTitleColor:[UIColor colorWithHexString:@"#FE3A2F"] forState:UIControlStateSelected];
    [redBtn setTitleColor:[UIColor colorWithHexString:@"#FE3A2F"] forState:UIControlStateHighlighted];
    [backView addSubview:redBtn];
    
    UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, redBtn.bottom, kScreenWidth - 30, DimensionBaseIphone6(48))];
    confirmBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:16];
    [confirmBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateSelected];
    [confirmBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateHighlighted];
    [confirmBtn setTitle:@"Confirming" forState:UIControlStateNormal];
    [confirmBtn setTitle:@"Confirming" forState:UIControlStateSelected];
    [confirmBtn setTitle:@"Confirming" forState:UIControlStateHighlighted];
    [confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateNormal];
    [confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateSelected];
    [confirmBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateHighlighted];
    [confirmBtn addTarget:self action:@selector(clickConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:confirmBtn];
    
    self.fullNameFiled.text = esString(self.nameStr);
    self.IDNoFiled.text = esString(self.idNoStr);
    self.DateFiled.text = esString(self.dateStr);
    
}

- (void)showBottomView{
    kWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        
        weakSelf.wholeView.frame = CGRectMake(0, kScreenHeight - kWholeViewHeight, kScreenWidth, kWholeViewHeight);
         
     } completion:^(BOOL finished) {}];
    
}

- (void)clickCacel{
    [self endEditing:YES];
    kWeakSelf
    [UIView animateWithDuration:0.25 animations:^{
        
         weakSelf.wholeView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, kWholeViewHeight);
         
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"HideAlert" object:nil];
        
    }];
}

- (void)clickDate{
    [self endEditing:YES];
    DLog(@"clickDate");
    NSString *dateStr = [esString(self.DateFiled.text) stringByReplacingOccurrencesOfString:@"-" withString:@"/"];
    NSArray *dateArray = [esString(dateStr) componentsSeparatedByString:@"/"];
    
    if (dateArray.count == 3) {
        dateStr = [NSString stringWithFormat:@"%@/%@/%@",dateArray[2],dateArray[1],dateArray[0]];
    }
    DLog(@"====dateStr===%@",dateStr);
    YearMonthDayPickerView *picker = [[YearMonthDayPickerView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withCurrentSelectYearMonthDayString:dateStr withkOriginYear:1950];
    [[UIApplication sharedApplication].keyWindow addSubview:picker];
    [picker showBottomView];
    
    picker.callBackSelectDateBlock = ^(NSString * _Nonnull dateString) {
        DLog(@"======%@",dateString);
        NSArray *dateArray = [esString(dateString) componentsSeparatedByString:@"/"];
        DLog(@"---dateArray-----%@",dateArray);
        if (dateArray.count == 3) {
            self.DateFiled.text = [NSString stringWithFormat:@"%@/%@/%@",dateArray[2],dateArray[1],dateArray[0]];
        } else {
            self.DateFiled.text = esString(dateString);
        }
    };
   
   
    
    
}

- (void)clickConfirmBtn{
    if (![self.fullNameFiled.text notEmptyOrNull]) {
        [CreaditUtilsHelper boxShowWithMessage:@"Full Name"];
        return;
    }
    else if (![self.IDNoFiled.text notEmptyOrNull]) {
        [CreaditUtilsHelper boxShowWithMessage:@"ID No."];
        return;
    }
    else if (![self.DateFiled.text notEmptyOrNull]) {
        [CreaditUtilsHelper boxShowWithMessage:@"Date Of Birth"];
        return;
    }
   
    if (_callBackInfoStr) {
        _callBackInfoStr(self.fullNameFiled.text,self.IDNoFiled.text,self.DateFiled.text);
    }

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
